import 'package:flutter/material.dart';
import 'package:hi_flutter/im/chat/message.dart';
import 'package:hi_flutter/im/contacts/contacts.dart';
import 'package:hi_flutter/im/personal/personal.dart';

class ChatApp extends StatefulWidget {
  const ChatApp({super.key});

  @override
  State<StatefulWidget> createState() {
    return ChatAppState();
  }
}

class ChatAppState extends State<ChatApp> {
  int _currentIndex = 0;

  Message? message;
  Contacts? contacts;
  Personal? personal;

  //
  currentPage() {
    switch (_currentIndex) {
      case 0:
        message ??= Message();
        return message;
      case 1:
        contacts ??= Contacts();
        return contacts;
      case 2:
        personal ??= Personal();
        return personal;
    }
  }

  _popupMenuItem(String title, {String? imagePath, IconData? icon}) {
    return PopupMenuItem(
        child: Row(
      children: <Widget>[
        imagePath != null
            ? Image.asset(imagePath, width: 32.0, height: 32.0)
            : SizedBox(
                width: 32.0,
                height: 32.0,
                child: Icon(
                  icon,
                  color: Colors.white,
                )),
        Container(
          padding: const EdgeInsets.only(left: 20.0),
          child: Text(
            title,
            style: const TextStyle(color: Colors.white),
          ),
        )
      ],
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('即时通讯'),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.of(context).pushNamed('search');
            },
            child: const Icon(Icons.search),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 30.0, right: 30.0),
            child: GestureDetector(
              onTap: () {
                showMenu(
                    context: context,
                    position:
                        const RelativeRect.fromLTRB(500.0, 96.0, 10.0, 10.0),
                    items: <PopupMenuItem>[
                      _popupMenuItem('发起会话',
                          imagePath: 'images/icon_menu_group.png'),
                      _popupMenuItem('添加好友',
                          imagePath: 'images/icon_menu_addfriend.png'),
                      _popupMenuItem('联系客服',
                          imagePath: 'images/icon_menu_scan.png'),
                    ]);
              },
              child: const Icon(Icons.add),
            ),
          )
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: _currentIndex,
          onTap: ((index) {
            setState(() {
              _currentIndex = index;
            });
          }),
          unselectedLabelStyle: const TextStyle(color: Color(0xff999999)),
          selectedLabelStyle: const TextStyle(
            color: Color(0xFF46c01b),
          ),
          items: [
            BottomNavigationBarItem(
              icon: _currentIndex == 0
                  ? Image.asset(
                      'images/message_pressed.png',
                      width: 32.0,
                      height: 28.0,
                    )
                  : Image.asset(
                      'images/message_normal.png',
                      width: 32.0,
                      height: 28.0,
                    ),
              label: '聊天',
            ),
            BottomNavigationBarItem(
              icon: _currentIndex == 1
                  ? Image.asset(
                      'images/contact_list_pressed.png',
                      width: 32.0,
                      height: 28.0,
                    )
                  : Image.asset(
                      'images/contact_list_normal.png',
                      width: 32.0,
                      height: 28.0,
                    ),
              label: '好友',
            ),
            BottomNavigationBarItem(
              icon: _currentIndex == 2
                  ? Image.asset(
                      'images/profile_pressed.png',
                      width: 32.0,
                      height: 28.0,
                    )
                  : Image.asset(
                      'images/profile_normal.png',
                      width: 32.0,
                      height: 28.0,
                    ),
              label: '我的',
            ),
          ]),
      body: currentPage(),
    );
  }
}
